package test;

import org.junit.Test;

import sqlengine.Main;

public class CreateTable {

	static int score = 0;
	
	
	@Test
	public void CreateTable1(){
		boolean eCaught = false;
		String query = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String out = null;
		try{
			Main m = new Main();
			out = m.process(query);
		}catch(Exception e){
			eCaught = true;
		}
		
		if(out != null && out.equals("Table created.\n")){
			score+= Breakdown.Create1;
			SC.score.append("\n\t createTable builds a small table correctly - " + Breakdown.Create1 + "/" + Breakdown.Create1);
		}else{
			SC.score.append("\n\t createTable doesn't build a small table correctly - 0/" + Breakdown.Create1);
		}
	}
	
	@Test
	public void CreateTable2(){
		boolean eCaught = false;
		StringBuilder input = new StringBuilder();
		input.append("create table simple1 (");
		for(int i = 0; i < 100; i++){
			if(i != 30)
				input.append("f" + i + " varchar2(30), ");
			else
				input.append("f" + i + " varchar2(30) primary key, ");
		}
		input.append("f100 varchar2(30));\n");
		
		String out = null;
		try{
			Main m = new Main();
			out = m.process(input.toString());
		}catch(Exception e){
			eCaught = true;
		}
		
		if(out != null && out.equals("Table created.\n")){
			score+= Breakdown.Create2;
			SC.score.append("\n\t createTable builds a bigger table correctly - " + Breakdown.Create2 + "/" + Breakdown.Create2);
		}else{
			SC.score.append("\n\t createTable doesn't build a bigger table correctly - 0/" + Breakdown.Create2);
		}
	}
	
	
	@Test
	public void CreateTableEx1(){
		
		boolean eCaught = false;
		StringBuilder input = new StringBuilder();
		input.append("create table simple1 (");
		for(int i = 0; i < 100; i++){
			input.append("f" + i + " varchar2(30), ");
		}
		input.append("f100 varchar2(30));\n");
		
		String out = null;
		try{
			Main m = new Main();
			out = m.process(input.toString());
		}catch(Exception e){
			eCaught = true;
		}
		
		if(out != null && out.equals("Error\n")){
			score+= Breakdown.CreateEx1;
			SC.score.append("\n\t createTable checks for primary key column - " + Breakdown.CreateEx1 + "/" + Breakdown.CreateEx1);
		}else{
			SC.score.append("\n\t createTable doesn't check for primary key column - 0/" + Breakdown.CreateEx1);
		}	
	}
	
	@Test
	public void CreateTableVariousExceptions(){
		
		boolean eCaught = false;
		StringBuilder input = null;
		
		String out = null;
		try{
			
			input = new StringBuilder();
			input.append("create table simple1 (");
			for(int i = 0; i < 100; i++){
				if(i != 30)
					input.append("f" + i + " varchar2(30), ");
				else
					input.append("f" + i + " varchar2(30) primary key, ");
			}
			input.append("f100 varchar2(30));\n");
					
			Main m = new Main();
			out = m.process(input.toString());
			out = m.process(input.toString());
			if(out == null || !out.equals("Error\n")){
				throw new Exception();
			}
			//---
			input = new StringBuilder();
			input.append("create table simple1 (");
			for(int i = 0; i < 100; i++){
				if(i != 30)
					input.append("f" + i + " varchar2(30), ");
				else
					input.append("f" + i + " varchar2(30) primary key, ");
			}
			input.append("f45 varchar2(30));\n");
			m = new Main();
			out = m.process(input.toString());
			if(out == null || !out.equals("Error\n")){
				throw new Exception();
			}
			//---
			input = new StringBuilder();
			input.append("create table 1simple1 (");
			for(int i = 0; i < 100; i++){
				if(i != 30)
					input.append("f" + i + " varchar2(30), ");
				else
					input.append("f" + i + " varchar2(30) primary key, ");
			}
			input.append("f123 varchar2(30));");
			m = new Main();
			out = m.process(input.toString());
			if(out == null || !out.equals("Error\n")){
				throw new Exception();
			}
			//---
			input = new StringBuilder();
			input.append("create table simple1 (");
			for(int i = 0; i < 100; i++){
				if(i != 30)
					input.append("f" + i + " varchar2(30), ");
				else
					input.append("1f" + i + " varchar2(30) primary key, ");
			}
			input.append("f123 varchar2(30));");
			m = new Main();
			out = m.process(input.toString());
			if(out == null || !out.equals("Error\n")){
				throw new Exception();
			}
			//---
			input = new StringBuilder();
			input.append("create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n");
			m = new Main();
			out = m.process(input.toString());
			if(out == null || out.equals("Error\n")){
				throw new Exception();
			}
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught){
			score+= Breakdown.CreateEx2;
			SC.score.append("\n\t createTable checks for errors correctly - " + Breakdown.CreateEx2 + "/" + Breakdown.CreateEx2);
		}else{
			SC.score.append("\n\t createTable doesn't check for errors correctly  - 0/" + Breakdown.CreateEx2);
		}	
	}
	
}
